home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Python 1.4 / Python 1.4 source / Lib / macpath.py < prev    next >
Encoding:
Text File  |  1996-08-02  |  4.4 KB  |  192 lines  |  [TEXT/Pyth]

  1. # module 'macpath' -- pathname (or -related) operations for the Macintosh
  2.  
  3. import string
  4. import mac
  5. from stat import *
  6.  
  7.  
  8. # Normalize the case of a pathname.  Dummy in Posix, but string.lower here.
  9.  
  10. normcase = string.lower
  11.  
  12.  
  13. # Return true if a path is absolute.
  14. # On the Mac, relative paths begin with a colon,
  15. # but as a special case, paths with no colons at all are also relative.
  16. # Anything else is absolute (the string up to the first colon is the
  17. # volume name).
  18.  
  19. def isabs(s):
  20.     return ':' in s and s[0] <> ':'
  21.  
  22.  
  23. # Join two pathnames.
  24. # The result is equivalent to what the second pathname would refer to
  25. # if the first pathname were the current directory.
  26.  
  27. def join(s, t):
  28.     if (not s) or isabs(t): return t
  29.     if t[:1] == ':': t = t[1:]
  30.     if ':' not in s:
  31.         s = ':' + s
  32.     if s[-1:] <> ':':
  33.         s = s + ':'
  34.     return s + t
  35.  
  36.  
  37. # Split a pathname in two parts: the directory leading up to the final bit,
  38. # and the basename (the filename, without colons, in that directory).
  39. # The result (s, t) is such that join(s, t) yields the original argument.
  40.  
  41. def split(s):
  42.     if ':' not in s: return '', s
  43.     colon = 0
  44.     for i in range(len(s)):
  45.         if s[i] == ':': colon = i+1
  46.     return s[:colon-1], s[colon:]
  47.  
  48.  
  49. # Split a path in root and extension.
  50. # The extension is everything starting at the last dot in the last
  51. # pathname component; the root is everything before that.
  52. # It is always true that root + ext == p.
  53.  
  54. def splitext(p):
  55.     root, ext = '', ''
  56.     for c in p:
  57.         if c == ':':
  58.             root, ext = root + ext + c, ''
  59.         elif c == '.':
  60.             if ext:
  61.                 root, ext = root + ext, c
  62.             else:
  63.                 ext = c
  64.         elif ext:
  65.             ext = ext + c
  66.         else:
  67.             root = root + c
  68.     return root, ext
  69.  
  70.  
  71. # Split a pathname into a drive specification and the rest of the
  72. # path.  Useful on DOS/Windows/NT; on the Mac, the drive is always
  73. # empty (don't use the volume name -- it doesn't have the same
  74. # syntactic and semantic oddities as DOS drive letters, such as there
  75. # being a separate current directory per drive).
  76.  
  77. def splitdrive(p):
  78.     return '', p
  79.  
  80.  
  81. # Short interfaces to split()
  82.  
  83. def dirname(s): return split(s)[0]
  84. def basename(s): return split(s)[1]
  85.  
  86.  
  87. # Return true if the pathname refers to an existing directory.
  88.  
  89. def isdir(s):
  90.     try:
  91.         st = mac.stat(s)
  92.     except mac.error:
  93.         return 0
  94.     return S_ISDIR(st[ST_MODE])
  95.  
  96.  
  97. # Return true if the pathname refers to a symbolic link.
  98. # (Always false on the Mac, until we understand Aliases.)
  99.  
  100. def islink(s):
  101.     return 0
  102.  
  103.  
  104. # Return true if the pathname refers to an existing regular file.
  105.  
  106. def isfile(s):
  107.     try:
  108.         st = mac.stat(s)
  109.     except mac.error:
  110.         return 0
  111.     return S_ISREG(st[ST_MODE])
  112.  
  113.  
  114. # Return true if the pathname refers to an existing file or directory.
  115.  
  116. def exists(s):
  117.     try:
  118.         st = mac.stat(s)
  119.     except mac.error:
  120.         return 0
  121.     return 1
  122.  
  123. #
  124. # dummy expandvars to retain interface-compatability with other
  125. # operating systems.
  126. def expandvars(path):
  127.     return path
  128.  
  129. #
  130. # dummy expanduser to retain interface-compatability with other
  131. # operating systems.
  132. def expanduser(path):
  133.     return path
  134.  
  135. # Normalize a pathname: get rid of '::' sequences by backing up,
  136. # e.g., 'foo:bar::bletch' becomes 'foo:bletch'.
  137. # Raise the exception norm_error below if backing up is impossible,
  138. # e.g., for '::foo'.
  139. # XXX The Unix version doesn't raise an exception but simply
  140. # returns an unnormalized path.  Should do so here too.
  141.  
  142. norm_error = 'macpath.norm_error: path cannot be normalized'
  143.  
  144. def normpath(s):
  145.     import string
  146.     if ':' not in s:
  147.         return ':' + s
  148.     f = string.splitfields(s, ':')
  149.     pre = []
  150.     post = []
  151.     if not f[0]:
  152.         pre = f[:1]
  153.         f = f[1:]
  154.     if not f[len(f)-1]:
  155.         post = f[-1:]
  156.         f = f[:-1]
  157.     res = []
  158.     for seg in f:
  159.         if seg:
  160.             res.append(seg)
  161.         else:
  162.             if not res: raise norm_error, 'path starts with ::'
  163.             del res[len(res)-1]
  164.             if not (pre or res):
  165.                 raise norm_error, 'path starts with volume::'
  166.     if pre: res = pre + res
  167.     if post: res = res + post
  168.     s = res[0]
  169.     for seg in res[1:]:
  170.         s = s + ':' + seg
  171.     return s
  172.  
  173.  
  174. # Directory tree walk.
  175. # For each directory under top (including top itself),
  176. # func(arg, dirname, filenames) is called, where
  177. # dirname is the name of the directory and filenames is the list
  178. # of files (and subdirectories etc.) in the directory.
  179. # The func may modify the filenames list, to implement a filter,
  180. # or to impose a different order of visiting.
  181.  
  182. def walk(top, func, arg):
  183.     try:
  184.         names = mac.listdir(top)
  185.     except mac.error:
  186.         return
  187.     func(arg, top, names)
  188.     for name in names:
  189.         name = join(top, name)
  190.         if isdir(name):
  191.             walk(name, func, arg)
  192.